package com.lwx.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwx.bus.domain.Customer;
import com.lwx.bus.mapper.CustomerMapper;
import com.lwx.bus.service.CustomerService;
import com.lwx.sys.utils.DataGridView;
import com.lwx.sys.vo.CustomerVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cn.lwx
 * @since 2020-04-21
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public DataGridView loadAllCustomer(CustomerVo customerVo) {
        QueryWrapper<Customer> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(customerVo.getCustomername()),"customername",customerVo.getCustomername());
        qw.like(StringUtils.isNotBlank(customerVo.getTelephone()),"telephone",customerVo.getTelephone());
        qw.like(StringUtils.isNotBlank(customerVo.getConnectionperson()),"connectionperson",customerVo.getConnectionperson());
        qw.orderByDesc("id");
        //分页
        Page page = new Page<>(customerVo.getPage(), customerVo.getLimit());

        //查询数据库
        customerMapper.selectPage(page,qw);

        return new DataGridView(page.getTotal(),page.getRecords());
    }

    //增删改都要加事务
    @Transactional
    @Override
    public void deleteCustomer(Integer id) {

        customerMapper.deleteById(id);
    }

    @Transactional
    @Override
    public void batchDeleteCustomer(Integer[] ids) {
        customerMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Transactional
    @Override
    public void addCustomer(Customer customer) {
        customerMapper.insert(customer);
    }

    @Transactional
    @Override
    public void updateCustomer(Customer customer) {

        customerMapper.updateById(customer);
    }


}
